home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / save.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  5KB  |  201 lines

  1. /*
  2.     GWAda Development Environment for 386/486 PCs   
  3.     Copyright (C) 1993, Arthur Vargas Lopes  & Michael Bliss Feldman
  4.                         vlopes@vortex.ufrgs.br mfeldman@seas.gwu.edu
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; version 2 of the License.    
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. /* save.c */
  21.  
  22. #include "externs.h"
  23.  
  24.  
  25.  
  26.  
  27. void AVL_SAVE_AS()
  28. {
  29.     AVL_WIN_PTR win;
  30.     AVL_WIN_PTR m1, m2;
  31.     AVL_LINE_PTR temp, temp2 = NULL;
  32.     AVL_EDIT_WINDOW_PTR w;
  33.     int fp, i;
  34.     int line_no = 0;
  35.     long fsize;
  36.     char *buf;
  37.     char line[1024], l2[80];
  38.     unsigned int size, n, j;
  39.     int n1, n2, x, from_here = 0, till_here = 0, rp;
  40.     char fname[121];
  41.     static char *msg = " GWAda - Save As... ";
  42.     char msg2[80];
  43.     w = &avl_windows[avl_window];
  44.     n1 = 62;
  45.     n2 = (80 - n1) / 2;
  46.     m1 = AVL_MAKE_WINDOW(msg,7,n2,9,n1+n2+2,avl_wnd_bk_color,avl_wnd_color);
  47.     fname[0] = '\0';
  48.     rp = AVL_PROMPT(1,1,fname,60);
  49.     AVL_DEL_WINDOW(m1);
  50.     if (rp)
  51.         return;
  52.     w = &avl_windows[avl_window];
  53.     w -> changed = 0;
  54.     sprintf(line,"Writing %s ...", fname);
  55.     win = AVL_MAKE_WINDOW(" Save As ...",23,1,25,strlen(line)+3,avl_wnd_bk_color,avl_wnd_color);
  56.     _setbkcolor(avl_msg_bk_color);
  57.     _settextcolor(avl_msg_color);
  58.     _settextposition(1,1);
  59.     _outtext(line);
  60.     sprintf(l2,"%s", fname);
  61.     for(i = 0; i < strlen(l2) && l2[i] != '.'; ++i)  
  62.         line[i] = fname[i];
  63.     line[i] = '.';
  64.     line[i + 1] = 'b';
  65.     line[i + 2] = 'a';
  66.     line[i + 3] = 'k';
  67.     line[i + 4] = '\0';
  68.     unlink(line);
  69.     rename(l2, line);
  70.     fp = open(l2,O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
  71.     if (fp == -1)  {
  72.         sprintf(line,"Could not open \'%s\'\n", l2);
  73.         AVL_ERROR(line);
  74.         AVL_DEL_WINDOW(win);
  75.         return;
  76.         }
  77.     size = 100000L;
  78.     if ((buf = malloc(size)) == NULL)   {
  79.         AVL_ERROR("Out of memory. Can't write file to disk!");
  80.         AVL_DEL_WINDOW(win);
  81.         return;
  82.         }
  83.     /* FIll in buffer */
  84.     j = 0;
  85.     temp = w -> head -> next;
  86.     while (temp != w -> head)  {
  87.         if (j >= size)  {  /*  flush buffer to disk  */
  88.             n = write(fp,buf,size);
  89.             if (n != size) {
  90.                 AVL_ERROR("Disk full. Make room and then try again!");
  91.                 close(fp);
  92.                 unlink(fname);
  93.                 rename(line, fname);
  94.                 AVL_DEL_WINDOW(win);
  95.                 return;
  96.                 }
  97.             fsize -= n;
  98.             j = 0;
  99.             }
  100.         for(i = 0; i < strlen(temp -> line); ++i)
  101.             buf[j++] = temp -> line[i];
  102.         if (temp -> next != w -> head)
  103.             buf[j++] = '\n';
  104.         temp = temp -> next;
  105.         } 
  106.     n = write(fp,buf,j);
  107.     if (n != j) {
  108.         AVL_ERROR("Disk full. Make room and then try again!");
  109.         close(fp);
  110.         unlink( fname );
  111.         rename(line, fname );
  112.         AVL_DEL_WINDOW(win);
  113.         return;
  114.         }
  115.     close(fp);
  116.     free(buf);
  117.     AVL_DEL_WINDOW(win);
  118. }
  119.  
  120. void AVL_SAVE()
  121. {
  122.     AVL_EDIT_WINDOW_PTR w;
  123.     AVL_WIN_PTR win;
  124.     AVL_LINE_PTR temp, temp2 = NULL;
  125.     int fp, i;
  126.     int line_no = 0;
  127.     long fsize;
  128.     char *buf;
  129.     char line[1024], l2[80];
  130.     unsigned int size, n, j;
  131.     w = &avl_windows[avl_window];
  132.     w -> changed = 0;
  133.     sprintf(line,"Writing %s ...", w -> file_name);
  134.     win = AVL_MAKE_WINDOW("",23,1,25,strlen(line)+3,avl_wnd_bk_color,avl_wnd_color);
  135.     _setbkcolor(avl_msg_bk_color);
  136.     _settextcolor(avl_msg_color);
  137.     _settextposition(1,1);
  138.     _outtext(line);
  139.     sprintf(l2,"%s",w -> file_name);
  140.     for(i = 0; i < strlen(l2) && l2[i] != '.'; ++i)  
  141.         line[i] = w -> file_name[i];
  142.     line[i] = '.';
  143.     line[i + 1] = 'b';
  144.     line[i + 2] = 'a';
  145.     line[i + 3] = 'k';
  146.     line[i + 4] = '\0';
  147.     unlink(line);
  148.     rename(l2, line);
  149.     fp = open(l2,O_CREAT | O_RDWR,S_IREAD | S_IWRITE);
  150.     if (fp == -1)  {
  151.         sprintf(line,"Could not open \'%s\'\n", l2);
  152.         AVL_ERROR(line);
  153.         AVL_DEL_WINDOW(win);
  154.         return;
  155.         }
  156.     size = 100000L;
  157.     if ((buf = malloc(size)) == NULL)   {
  158.         AVL_ERROR("Out of memory. Can't write file to disk!");
  159.         AVL_DEL_WINDOW(win);
  160.         return;
  161.         }
  162.     /* FIll in buffer */
  163.     j = 0;
  164.     temp = w -> head -> next;
  165.     while (temp != w -> head)  {
  166.         if (j >= size)  {  /*  flush buffer to disk  */
  167.             n = write(fp,buf,size);
  168.             if (n != size) {
  169.                 AVL_ERROR("Disk full. Make room and then try again!");
  170.                 close(fp);
  171.                 unlink(w -> file_name);
  172.                 rename(line, w -> file_name);
  173.                 AVL_DEL_WINDOW(win);
  174.                 return;
  175.                 }
  176.             fsize -= n;
  177.             j = 0;
  178.             }
  179.         for(i = 0; i < strlen(temp -> line); ++i)
  180.             buf[j++] = temp -> line[i];
  181.         if (temp -> next != w -> head)
  182.             buf[j++] = '\n';
  183.         temp = temp -> next;
  184.         } 
  185.     n = write(fp,buf,j);
  186.     if (n != j) {
  187.         AVL_ERROR("Disk full. Make room and then try again!");
  188.         close(fp);
  189.         unlink(w -> file_name);
  190.         rename(line, w -> file_name);
  191.         AVL_DEL_WINDOW(win);
  192.         return;
  193.         }
  194.     close(fp);
  195.     free(buf);
  196.     AVL_DEL_WINDOW(win);
  197. }
  198.  
  199.  
  200.  
  201.